/*
 * CS_LAST_WEEK_RANK_REWARD.cpp
 *
 *  Created on: 2013. 6. 10.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Reward.h"
#include "../../NLogic/Function.h"
#include "../../NResource/Static.h"
#include "../../NResource/WeeklyRewardResource.h"
#include "../../Proxy.h"
#include "../../../../protocol/iFriends.h"



namespace ProjectZ { namespace NCommand {

void SendLastWeekRewardFail(User * pUser, int result)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(result) << static_cast<uint8_t>(0);
	pAck->MakePacket(CMD_SC_LAST_WEEK_RANK_REWARD);
	pUser->Send(pAck);
}

void SendMailLastWeekReward(User * pUser, NLogic::Item::Data & item, int gold, int rewardType)
{
	NLogic::MailBox::Mail::Data clsMailData;
	clsMailData.send_user_seq		= 0;
	clsMailData.send_user_nickname	= "GAMEVIL";
	clsMailData.recv_user_seq		= pUser->GetUserSeq();
	if(0 == rewardType)
	{
//		NResource::MailMessageResource::Flyweight *pFly = NResource::Static::InstancePtr()->GetMailMessageResource()->GetFly(NResource::MailMessageResource::EM_LAST_WEEK_RANKING_REWARD);
		clsMailData.message				= NResource::Static::InstancePtr()->GetMailMessageResource()->GetMessageWeeklyRankingReward();
	}
	else
	{
//		NResource::MailMessageResource::Flyweight *pFly = NResource::Static::InstancePtr()->GetMailMessageResource()->GetFly(NResource::MailMessageResource::EM_LAST_WEEK_FRIENDS_COUNT_REWARD);
//		clsMailData.message				= NResource::Static::InstancePtr()->GetMailMessageResource()->GetMessageWeeklyFriendsCountReward();
	}

	if(0 < gold)
		clsMailData.gift_status		= EM_GIFT_GOLD;
	else
		clsMailData.gift_status		= EM_GIFT_ITEM;
	clsMailData.item				= item;
	clsMailData.gold				= gold;

	if (false == NLogic::NFunction::SendMail(pUser, clsMailData))
	{
		DEBUGLOG("%s : false == NLogic::NFunction::SendMail", __FUNCTION__);
		SendLastWeekRewardFail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

//	NLogic::MailBox::Mail * pMail	= NLogic::Static::InstancePtr()->GETMAILPOOL()->NEW();
//	pMail->Open_Normal(clsMailData, pUser);
//
//	if (false == pMail->InsertDatabaseMail())
//	{
//		NLogic::Static::InstancePtr()->GETMAILPOOL()->DEL(pMail);
//		DEBUGLOG("%s : false == pMail->InsertDatabaseMail()!! ITEM", __FUNCTION__);
//		SendLastWeekRewardFail(pUser, ACK_UNKNOWN_ERROR);
//		return;
//	}
//	NLogic::Static::InstancePtr()->GETMAILPOOL()->DEL(pMail);
}
/*
/// 주간 랭킹 보상 받기
ZNO_CS_LAST_WEEK_RANK_REWARD	0x2806
{
	//u8	u8reward_type;		// 0: 랭킹 확인(단순 랭킹만 확인 보상없음) 1: 보상 받음
	//u16	u16rank;		// 보상없을시 0
	//u32	u32point;		// 보상없을시 0
	u8 u8reward_info_type; 	//0: 랭킹 보상, 1: 참여 친구 보상

}

ZNO_SC_LAST_WEEK_RANK_REWARD	0x2807
{
	s16	ack;
	//u8	u8reward_type;		// 0: 랭킹 확인(단순 랭킹만 확인 보상없음) 1: 보상 받음
	//u8	u8reward_value_type;	// 0 : 골드, 1: 아이템
	//u64	u64reward_value;	// 골드일시는 금액, 아이템일시는 시컨스
	//u8	u8bag_type;
	u8 u8reward_info_type; 	//0: 랭킹 보상, 1: 참여 친구 보상
}
*/
void Command::CS_LAST_WEEK_RANK_REWARD(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	uint8_t rewardInfoType = -1;

	* pPacket >> rewardInfoType;
	DEBUGLOG("rewardInfoType %d", rewardInfoType);

	NLogic::Reward::Data * lastWeekRankReward = pUser->GetReward()->GetData(NLogic::Reward::RewardType::LAST_WEEK_RANK_REWARD);
	if(NULL == lastWeekRankReward)
	{
		DEBUGLOG("NULL == lastWeekRankReward");
		SendLastWeekRewardFail(pUser, ACK_NOT_EXIST_REWARD);
		return;
	}
	NLogic::Reward::Data * lastWeekRankFriend = pUser->GetReward()->GetData(NLogic::Reward::RewardType::LAST_WEEK_RANK_FRIEND);

	if(0 == rewardInfoType)
	{
		if(0 != lastWeekRankReward->_value)
		{
	DEBUGLOG("rewardInfoType %d lastWeekRankReward->_value %d", rewardInfoType, lastWeekRankReward->_value);
			SendLastWeekRewardFail(pUser, ACK_NOT_EXIST_REWARD);
			return;
		}
	}
	else if(1 == rewardInfoType)
	{
		if(0 != lastWeekRankFriend->_value)
		{
	DEBUGLOG("rewardInfoType %d lastWeekRankFriend->_value %d", rewardInfoType, lastWeekRankFriend->_value);
			SendLastWeekRewardFail(pUser, ACK_NOT_EXIST_REWARD);
			return;
		}
	}

	bool bReward = false;
	NResource::WeeklyRewardData::DATATREE & pRewardData = NResource::Static::InstancePtr()->GetWeeklyReward()->GetFly(pUser)->GetFlyTree();

	time_t curtime = time(NULL);
//	std::for_each(pRewardData.begin(), pRewardData.end(), [&](std::pair<NResource::WeeklyRewardData::Key, NResource::WeeklyRewardData::Flyweight*> pair) {
	auto iter = pRewardData.begin();
	for(; iter != pRewardData.end(); ++iter)
	{
		if( 0 == rewardInfoType && 0 == iter->second->_TYPE)
		{
			if(iter->second->_MIN <= static_cast<int>(pUser->GetUserInfo().score)
					&& static_cast<int>(pUser->GetUserInfo().score) <= iter->second->_MAX)
			{
//				NResource::ChargeItemData::Flyweight * chargeItemData = NResource::Static::InstancePtr()->GetChargeItem()->GetFly(pUser, iter->second->_REWARD_TYPE);

				NLogic::Item::Data clsData;
				clsData.tid = iter->second->_REWARD_TID;
				clsData.quantity = iter->second->_VALUE;
				clsData.class_type = -1;
				clsData.sub_type = iter->second->_REWARD_SHEET_TYPE;
				NResource::Static::InstancePtr()->GetItemResource()->SetItemDataFromResource(pUser, clsData);
				SendMailLastWeekReward(pUser, clsData, iter->second->_VALUE, rewardInfoType);
				lastWeekRankReward->_value = 1;
				bReward = true;
				DEBUGLOG("내 랭크 %d", pUser->GetUserInfo().score);
				pUser->GetUserInfo().array_Slot[static_cast<int>(NLogic::Reward::RewardType::LAST_WEEK_RANK_REWARD)].reg_date = curtime;
			}
//			else if(iter->second->_MIN == iter->second->_MAX && iter->second->_MAX <= pUser->GetUserInfo().score)
//			{
//				NLogic::Item::Data clsData;
//				clsData.tid = iter->second->_REWARD_TID;
//				clsData.quantity = iter->second->_VALUE;
//				clsData.class_type = -1;
//				clsData.sub_type = iter->second->_REWARD_SHEET_TYPE;
//				NResource::Static::InstancePtr()->GetItemResource()->SetItemDataFromResource(pUser, clsData);
//				SendMailLastWeekReward(pUser, clsData, iter->second->_VALUE);
//				lastWeekRankReward->_rewardReceiveTime = time(NULL);
//				lastWeekRankReward->_value = 1;
//				bReward = true;
//			}
		}
		else if(1 == rewardInfoType && 1 == iter->second->_TYPE)
		{
			DEBUGLOG("min %d 참여 친구 수 %d max %d",iter->second->_MIN, pUser->GetUserInfo().heart_blocked, iter->second->_MAX);
			if(iter->second->_MIN <= static_cast<int>(pUser->GetUserInfo().heart_blocked)
					&& static_cast<int>(pUser->GetUserInfo().heart_blocked) <= iter->second->_MAX)
			{
				NLogic::Item::Data clsData;
				SendMailLastWeekReward(pUser, clsData, iter->second->_VALUE, rewardInfoType);
				lastWeekRankFriend->_value = 1;
				bReward = true;
				pUser->GetUserInfo().array_Slot[static_cast<int>(NLogic::Reward::RewardType::LAST_WEEK_RANK_FRIEND)].reg_date = curtime;
			}
			else if(iter->second->_MIN == iter->second->_MAX
					&& iter->second->_MAX <= static_cast<int>(pUser->GetUserInfo().heart_blocked))
			{
				NLogic::Item::Data clsData;
				SendMailLastWeekReward(pUser, clsData, iter->second->_VALUE, rewardInfoType);
				lastWeekRankFriend->_value = 1;
				bReward = true;
				pUser->GetUserInfo().array_Slot[static_cast<int>(NLogic::Reward::RewardType::LAST_WEEK_RANK_FRIEND)].reg_date = curtime;
			}
		}
	}
//	std::for_each(pRewardData.begin(), pRewardData.end(), [&](std::pair<int, NResource::WeeklyRewardData::Flyweight*> pair) {
//		if(rewardInfoType == pair.second->_TYPE)
//		{
//			if(pair.second->_MIN <= pUser->GetUserInfo().score
//					&& pUser->GetUserInfo().score <= pair.second->_MAX)
//			{
//				NResource::ChargeItemData::Flyweight * chargeItemData = NResource::Static::InstancePtr()->GetChargeItem()->GetFly(pUser, pair.second->_REWARD_TYPE);
//
//				NLogic::Item::Data clsData;
//				clsData.tid = chargeItemData->_TID;
//				clsData.quantity = pair.second->_VALUE;
//				clsData.class_type = chargeItemData->_CLASSTYPE;
//				clsData.sub_type = chargeItemData->_SHEET;
//				NResource::Static::InstancePtr()->GetItemResource()->SetItemDataFromResource(pUser, clsData);
//				SendMailLastWeekReward(pUser, clsData, 0);
//				lastWeekRankReward->_value = 1;
//				bReward = true;
//			}
//			else if(pair.second->_MIN == pair.second->_MAX)
//			{
//				NResource::ChargeItemData::Flyweight * chargeItemData = NResource::Static::InstancePtr()->GetChargeItem()->GetFly(pUser, pair.second->_REWARD_TYPE);
//
//				NLogic::Item::Data clsData;
//				clsData.tid = chargeItemData->_TID;
//				clsData.quantity = pair.second->_VALUE;
//				clsData.class_type = chargeItemData->_CLASSTYPE;
//				clsData.sub_type = chargeItemData->_SHEET;
//				NResource::Static::InstancePtr()->GetItemResource()->SetItemDataFromResource(pUser, clsData);
//				SendMailLastWeekReward(pUser, clsData, 0);
//				lastWeekRankReward->_value = 1;
//				bReward = true;
//			}
//		}
//		else if(rewardInfoType == pair.second->_TYPE)
//		{
//			if(pair.second->_MIN <= lastWeekRankReward->_lastWeekFriendCount
//					&& lastWeekRankReward->_lastWeekFriendCount <= pair.second->_MAX)
//			{
//				NLogic::Item::Data clsData;
//				SendMailLastWeekReward(pUser, clsData, pair.second->_VALUE);
//				lastWeekRankFriend->_value = 1;
//				bReward = true;
//			}
//			else if(pair.second->_MIN == pair.second->_MAX)
//			{
//				NLogic::Item::Data clsData;
//				SendMailLastWeekReward(pUser, clsData, pair.second->_VALUE);
//				lastWeekRankFriend->_value = 1;
//				bReward = true;
//			}
//		}
//	});

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	if(bReward)
		* pAck << static_cast<int16_t>(ACK_OK);
	else
		* pAck << static_cast<int16_t>(ACK_NOT_EXIST_REWARD);

	* pAck << static_cast<uint8_t>(rewardInfoType);

	pAck->MakePacket(CMD_SC_LAST_WEEK_RANK_REWARD);
	pUser->Send(pAck);
	pUser->FlushUserInfoCache();
}

} /*NCommand*/ } /*ProjectZ*/


